#include "OnWaterState.h"
#include "Player.h"
#include <math.h>
#include "ShareComponents.h"
#include "FWKeyboard.h"
#include "RSManager.h"
#include "Box.h"
#define CONTRA_VX 2.4f 
#define CONTRA_VY 8.5f


OnWaterState::OnWaterState(Player* player)
{
	p = player;//
	this->_sSwimLeft = RSManager::getInstance()->getSprite(PLAYER_SWIM_LEFT);
	this->_sSwimRight = RSManager::getInstance()->getSprite(PLAYER_SWIM_RIGHT);

	this->_sSwimShootLeft = RSManager::getInstance()->getSprite(PLAYER_SWIM_SHOOT_LEFT);
	this->_sSwimShootRight = RSManager::getInstance()->getSprite(PLAYER_SWIM_SHOOT_RIGHT);

	this->_sSwimShootTopLeft = RSManager::getInstance()->getSprite(PLAYER_SWIM_S_TOP_LEFT);
	this->_sSwimShootTopRight = RSManager::getInstance()->getSprite(PLAYER_SWIM_S_TOP_RIGHT);

	this->_sSwimTopLeft = RSManager::getInstance()->getSprite(PLAYER_SWIM_II);
	this->_sSwimTopRight = RSManager::getInstance()->getSprite(PLAYER_SWIM_I);

	this->_sSwimDown = RSManager::getInstance()->getSprite(PLAYER_SWIM_DOWN);

	gDirection = RIGHT;
	jDirection = UP;
	p->setSprite(_sSwimRight);
	timeDelay = 180;
	_isDive = false;
}


PLAYER_STATE OnWaterState::handleInput(float time)
{
	p->set_vx(0);
	KBState = 0;
	_isDive = false;
	if (gl_keyboard->isKeyDown(DIK_D))	
		KBState = KBState | D;					
	if (gl_keyboard->isKeyDown(DIK_W))	
		KBState = KBState | W;					
	if (gl_keyboard->isKeyDown(DIK_A))	
		KBState = KBState | A;					
	if (gl_keyboard->isKeyDown(DIK_S))	
	{
		KBState = KBState | S;		
		_isDive = true;
	}
	if (gl_keyboard->isKeyPressed(DIK_U))	
		KBState = KBState | _U;		
	if(p->getBulletID() == B_M)
	{
		if(gl_keyboard->isKeyDown(DIK_I))		
			KBState = KBState | I;		
	}
	else
	{
		if(gl_keyboard->isKeyPressed(DIK_I))		
			KBState = KBState | I;			
	}

	switch (KBState)
	{
	case A:
		if(!setDelay(time))
			p->setSprite(_sSwimLeft);
		p->set_vx(-CONTRA_VX);
		gDirection = LEFT;
		break;
	case D:
		if(!setDelay(time))
			p->setSprite(_sSwimRight);
		p->set_vx(CONTRA_VX);			
		gDirection = RIGHT;
		break;
	case S:
		p->setSprite(_sSwimDown);
		break;
	case I:
		if(gDirection == LEFT)
		{
			p->setSprite(_sSwimShootLeft);
			p->shoot(0, 3, M_PI);
		}
		else
		{
			p->setSprite(_sSwimShootRight);
			p->shoot(38, 3, 0);
		}
		timeDelay = 0;
		break;
	case AS:
		p->setSprite(_sSwimDown);
		break;
	case AW:
		if(!setDelay(time))
			p->setSprite(_sSwimLeft);
		p->set_vx(-CONTRA_VX);
		gDirection = LEFT;		
		break;
	case DW:
		if(!setDelay(time))
			p->setSprite(_sSwimRight);
		p->set_vx(CONTRA_VX);			
		gDirection = RIGHT;		
		break;
	case DS:
		p->setSprite(_sSwimDown);
		break;
	case DI:
		p->setSprite(_sSwimShootRight);
		p->set_vx(CONTRA_VX);
		p->shoot(30, 3, 0);
		gDirection = RIGHT;
		timeDelay = 0;
		break;
	case AI:
		p->setSprite(_sSwimShootLeft);
		p->set_vx(-CONTRA_VX);
		p->shoot(0, 3, M_PI);
		gDirection = LEFT;
		timeDelay = 0;
		break;
	case WI:
		if(gDirection == LEFT)
		{
			p->setSprite(_sSwimShootTopLeft);
			p->shoot(0, 50, M_PI_2);
		}
		else
		{
			p->setSprite(_sSwimShootTopRight);
			p->shoot(20, 50, M_PI_2);
		}		
		timeDelay = 0;
		break;
	case DWI:
		p->setSprite(_sSwimTopRight);
		p->set_vx(CONTRA_VX);
		p->shoot(30, 40, M_PI/4);
		gDirection = RIGHT;
		timeDelay = 0;
		break;		
	case AWI:
		p->setSprite(_sSwimTopLeft);
		p->set_vx(-CONTRA_VX);
		p->shoot(0, 40, 3*M_PI/4);
		gDirection = LEFT;
		timeDelay = 0;
		break;
	default:
		if(!setDelay(time))
		{
			if(gDirection == LEFT)
				p->setSprite(_sSwimLeft);
			else
				p->setSprite(_sSwimRight);
		}
		break;
	}
	setCollisionBox();
	return ONWATER;
}

void OnWaterState::setCollisionBox()
{	
	if(_isDive)
	{
		collsionBox->_size.x = 0;
		collsionBox->_size.y = 0;
	}
	else
	{
		collsionBox->_size.x = 35;
		collsionBox->_size.y = 35;
	}
	collsionBox->_position = p->getBox()->_position;
	collsionBox->_v = p->getBox()->_v;	
	p->setCollisionBox(collsionBox);
}
